<?php


    namespace addons\zpwxsys\controller\v1;

    use addons\zpwxsys\controller\BaseController;

    use addons\zpwxsys\model\Adv as BannerModel;
    use addons\zpwxsys\model\Nav as NavModel;
    use addons\zpwxsys\model\City as CityModel;
    use addons\zpwxsys\model\Company as CompanyModel;

    use addons\zpwxsys\model\Job as JobModel;
    use addons\zpwxsys\model\Jobrecord as JobrecordModel;
    use addons\zpwxsys\model\Note as NoteModel;
    use addons\zpwxsys\model\Sysmsg as SysmsgModel;
    use addons\zpwxsys\model\User as UserModel;
    use addons\zpwxsys\service\AccessToken as AccessToken;
    use addons\zpwxsys\model\Sysinit as SysinitModel;
    use addons\zpwxsys\model\Notice as NoticeModel;
    use addons\zpwxsys\model\Worktype as WorktypeModel;
    use addons\zpwxsys\model\Money as MoneyModel;
    use addons\zpwxsys\model\Oldhouse as OldhouseModel;
    use addons\zpwxsys\service\LookRoleRecord as LookRoleRecordService;

    use addons\zpwxsys\service\WXBizDataCrypt;
    use addons\zpwxsys\service\Token;



    class Sysinit extends BaseController
    {

        public function getSysinit()
        {


            $city = input('post.city');


            $cityinfo = CityModel::getCityByName($city);


            $mapbanner['enabled'] = 1;
            $mapbanner['cityid'] = $cityinfo['id'];

            $bannerlist = BannerModel::getBanner($mapbanner);
            $navlist = NavModel::getNav();


            $JobModel = new JobModel();

            $od = "g.sort desc";
            $map['g.isrecommand'] = 1;
            $map['g.status'] = 1;
            $map['g.cityid'] = $cityinfo['id'];
            $limits = 10;
            $Nowpage = 1;
            $CompanyModel = new CompanyModel();
            $companylist = $CompanyModel->getCompanyByWhere($map, $Nowpage, $limits, $od);


            if ($companylist) {
                foreach ($companylist as $k => $v) {
                    $maps['companyid'] = $v['id'];

                    $companylist[$k]['jobcount'] = $JobModel->getJobTotal($maps);

                }

            }


            $sysinfo = SysinitModel::getSysinfo();
            $oldhouselist =[];
            if($sysinfo) {

                $param['id'] = $sysinfo['id'];
                $param['view'] = $sysinfo['view'] + 1;
                $sysinitModel = new SysinitModel();
                $sysinitModel->updateSysView($param);

                //用于过审
                if($sysinfo['ischeck'] == 1)
                {

                    $limits = 10;
                    $Nowpage = 1;
                    $od="h.sort desc";
                    $housemap['h.status'] = 0 ;

                    $OldhouseModel = new OldhouseModel();
                    $oldhouselist = $OldhouseModel->getHouseByWhere($housemap, $Nowpage, $limits,$od);

                }

            }


            $mapnotice['status'] = 1;
            $odnotice = "createtime desc";
            $limits = 20;
            $Nowpage = 1;

            $NoticeModel = new NoticeModel();
            $noticelist = $NoticeModel->getNoticeByWhere($mapnotice, $Nowpage, $limits, $odnotice);

            $mapwork['enabled'] = 1;
            $od = 'sort desc';
            $worktyelist = WorktypeModel::getList($mapwork, $od);
            $ret = \app\common\model\Crowd::find();


            $data = array('bannerlist' => $bannerlist, 'navlist' => $navlist, 'cityinfo' => $cityinfo, 'companylist' => $companylist, 'sysinfo' => $sysinfo, 'noticelist' => $noticelist, 'worktyelist' => $worktyelist,'oldhouselist'=>$oldhouselist,'workCrowd'=>$ret);


            return json_encode($data);


        }

        /**
         * @return false|string
         * @throws \addons\zpwxsys\lib\exception\ParameterException
         * @throws \think\Exception
         * 用户是否在职 ；is_currentstatus 0=未知；1=离职；2=在职
         */
        public function getUserinit()
        {
            $uid = Token::getCurrentUid();

            $map['uid'] = $uid;

            $umap['id'] = $uid;

            $userinfo =  UserModel::getByUserWhere($umap);

            if (!$userinfo) {
               return json_encode(['status'=>1,'msg'=>'请求数据不存在']);
            }
            $userinfo['is_currentstatus'] = 0;//未知
            $noteinfo = NoteModel::getNoteByuid($uid);
            if($noteinfo){
                if($noteinfo['currentstatus']=='我目前已离职,可快速到岗'){
                    $userinfo['is_currentstatus'] = 1;//离职
                }elseif ($noteinfo['currentstatus']=='我目前在职，但考虑换个新环境'){
                    $userinfo['is_currentstatus'] = 2;//在职
                }elseif ($noteinfo['currentstatus']=='观望有好的机会再考虑'){
                    $userinfo['is_currentstatus'] = 2;//在职
                }elseif ($noteinfo['currentstatus']=='目前暂无跳槽打算'){
                    $userinfo['is_currentstatus'] = 2;//在职
                }elseif ($noteinfo['currentstatus']=='应届毕业生'){
                    $userinfo['is_currentstatus'] = 2;//在职
                }
            }

            if($userinfo['avatarUrl']!='')
            {
                $isuser = true;
            }else{

                $isuser =false;
            }

            $sendnotecount = JobrecordModel::getJobrecordToal($map);
            $map['status'] = 1;
            $noticenotecount = JobrecordModel::getJobrecordToal($map);

            $lookrolerecodservice = new LookRoleRecordService($uid, 0);

            $rolerecord = $lookrolerecodservice->GetFirstRecord();

            if (!$rolerecord) {

                $totallooknum = 0;

            } else {

                $totallooknum = $rolerecord[0]['totallooknum'];

            }

            $mapsys['uid'] = $uid;
            $mapsys['status'] = 0;
            $sysmsgcount = SysmsgModel::getTotal($mapsys);

            $tmap['uid'] = $uid;
            $lastmoney =  MoneyModel::getLastOne($tmap);//最新的余额

            if($lastmoney)
            {
                $totalmoney = $lastmoney[0]['totalmoney'];
                $usermoney =$totalmoney;

            }else{

                $totalmoney = '0.00';
                $usermoney = 0 ;

            }


            $data = array('sendnotecount' => $sendnotecount, 'noticenotecount' => $noticenotecount, 'totallooknum' => $totallooknum, 'sysmsgcount' => $sysmsgcount,'isuser'=>$isuser,'userinfo'=>$userinfo,'totalmoney'=>$totalmoney,'usermoney'=>$usermoney,'status'=>0,'msg'=>'请求数据正常');

            return json_encode($data);


        }



        public function  getMoneyRecord()
        {
            $uid = Token::getCurrentUid();
            $map['m.uid'] = $uid;
            $od = 'm.createtime desc';
            $MoneyModel = new MoneyModel();


            $moneylist=$MoneyModel->getListOrderByWhere($map,$od);

            if($moneylist)
            {

                foreach ($moneylist as $k=>$v)
                {

                    if($v['type'] == 0 )
                    {

                    }elseif($v['type'] == -1)
                    {
                        $moneylist[$k]['orderinfo'] ='提现';

                    }


                    $moneylist[$k]['createtime'] = date('Y-m-d',$v['createtime']);
                }
            }


            $data = array('error'=>0,'moneylist'=>$moneylist);

            return json_encode($data);
        }


        public function getUserMoney()
        {
            $uid = Token::getCurrentUid();
            $map['uid'] = $uid;
            $lastmoney =  MoneyModel::getLastOne($map);//最新的余额

            if($lastmoney)
            {
                $totalmoney = $lastmoney[0]['totalmoney'];

                $usermoney = $totalmoney ;

            }else{

                $usermoney =  0;

            }

            if($usermoney>0)
            {
                $totalmoney = $totalmoney - $usermoney;

                $param_money['money'] = -$usermoney;

                $param_money['totalmoney'] = $totalmoney;

                $param_money['uid'] = $uid;

                $param_money['type'] = -1;

                $param_money['orderid'] = -1;

                $param_money['content'] = '佣金提现';

                $param_money['createtime'] = time();

                $moneyModel = new MoneyModel();

                $moneyModel->insertMoney($param_money);

                $data = array('error'=>0,'msg'=>'提现成功');

            }else{
                $data = array('error'=>1,'msg'=>'提现失败,余额不足');
            }


            return json_encode($data);



        }




        public function getPhone()
        {

            $config = get_addon_config('zpwxsys');


            $encryptedData = $this->request->post('encryptedData', '', 'trim');
            $iv = $this->request->post('iv');

            if (!$encryptedData || !$iv) {
                $this->error('参数错误！');
            }
            if (strlen($iv) != 24) {
                $this->error('iv不正确！');
            }

            $appid = $config['wxappid'];
            $appsecret = $config['wxappsecret'];

            $session_key = cache('session_key');


            $pc = new WXBizDataCrypt($appid, $session_key);

            $errCode = $pc->decryptData($encryptedData, $iv, $data);
            $obj = json_decode($data);


            $tel = $obj->phoneNumber;

            $data = array('msg' => '更新成功', 'status' => 0, 'tel' => $tel);

            return json_encode($data);


        }


        public function updateUsertel()
        {

            $uid = Token::getCurrentUid();
            $tel = input('post.tel');


             $userinfo  = UserModel::getByUserWhere(['id'=>$uid]);
             if (!$userinfo) {
               return json_encode(['status'=>1,'msg'=>'请求数据不存在']);
            }

            $userModel = new UserModel();

            $param['id'] = $uid;
            $param['tel'] = $tel;

            $userModel->Updateuser($param);
            $data = array('msg' => '更新成功', 'status' => 0);

            return json_encode($data);
        }

        public function getWxUserInfo()
        {
            $uid = Token::getCurrentUid();

            $map['id'] = $uid;

            $userinfo  = UserModel::getByUserWhere($map);

            if (!$userinfo) {
               return json_encode(['status'=>1,'msg'=>'请求数据不存在']);
            }

            if($userinfo['qrcode']== '')
            {

                $UserModel = new UserModel();

                $AccessToken = new AccessToken();
                $qrcode = $AccessToken->getUserQrcode($uid);



                $param['id'] = $uid;
                $param['qrcode'] = $qrcode;

                $UserModel->updateUser($param);

                $userinfo['qrcode'] = cdnurl($qrcode, true);



            }else{
                $userinfo['qrcode'] = cdnurl($userinfo['qrcode'], true);

            }
            $sysinfo = SysinitModel::getSysinfo();
            $userinfo['fxbg'] = cdnurl($sysinfo['fxbg'], true);
            $userinfo['sharebg'] = cdnurl($sysinfo['sharebg'], true);

            $data = array('userinfo'=>$userinfo);

            return json_encode($data);

        }

        public function updateUser()
        {
            $nickname = input('post.nickname');

            $avatarUrl = input('post.avatarUrl');

            $tel = input('post.tel');

            $lng = input('post.lng');
            $lat = input('post.lat');

            $uid = Token::getCurrentUid();

             $map['id'] = $uid;

            $userinfo  = UserModel::getByUserWhere($map);

            if (!$userinfo) {
               return json_encode(['status'=>1,'msg'=>'请求数据不存在']);
            }

            $userModel = new UserModel();

            $param['id'] = $uid;
            if($nickname){
                $param['nickname'] = $nickname;
            }
            if($avatarUrl){
                $param['avatarUrl'] = $avatarUrl;
            }
            if($tel){
                $param['tel'] = $tel;
            }

            if($lng&&$lat){
                $param['lng'] = $lng;
                $param['lat'] = $lat;
            }


            $param['update_time'] = time();


            $userModel->Updateuser($param);


        //$map['id'] = $uid;

           // $userinfo = UserModel::getByUserWhere($map);


            if ($userinfo['tel'] != '') {

                $istel = true;
            } else {

                $istel = false;
            }


            $data = array('msg' => '更新成功', 'status' => 0, 'istel' => $istel);

            return json_encode($data);


        }


    }
